﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Odbc;
using MySql.Data.MySqlClient;
using MySql.Data;
using System.Threading;
using MetaSearcher;

namespace Metasearcher
{
    class Global1
    {
        public DateTime[] date;
        public int[] price;
        public int count;
    }
    class Statistic1
    {
        public Global1 getDataForStat1(string resource, string city_from, string city_to, DateTime date_start, DateTime date_finish, int frequency)
        {
            string Connect = "Database=meta;Data Source=188.35.9.179;User Id=assa;Password=assa";
            int price = 0;
            Global1 gl_stat = new Global1();

            //Вычисление размерностей массивов выходных данных
            string getRecNum = "select count(distinct date_collect) from main where resource=" + "'" + resource + "'" + " and city_from=" + "'" + city_from + "'" + " and city_to=" + "'" + city_to + "'" + " and (SELECT CAST(date_dep AS DATE)) >= " + "'" + date_start.ToString("yyyy-MM-dd") + "'" + " and (SELECT CAST(date_dep AS DATE)) <= " + "'" + date_finish.ToString("yyyy-MM-dd") + "'" + " and (SELECT CAST(date_collect AS DATE)) = (SELECT CAST(date_dep AS DATE))" + ";";
            int cou = 0;
            MySqlConnection myConnection0 = new MySqlConnection(Connect);
            MySqlCommand getRec = new MySqlCommand(getRecNum, myConnection0);
            try
            {
                myConnection0.Open();

                MySqlDataReader countreader = getRec.ExecuteReader();
                countreader.Read();
                cou = countreader.GetInt32(0);
                countreader.Close();
                gl_stat.date = new DateTime[cou];
                gl_stat.date[0] = new DateTime(0001, 01, 01);
                gl_stat.price = new int[cou];
                gl_stat.price[0] = 0;
                myConnection0.Close();
            }
            catch (Exception ee) { int er1 = 1; }
            if (cou == 0)
            {
                Console.WriteLine("No data:returning null");
                return null;
            }
            else
            {
                //Заполнение массивов выходных данных
                int price_min = 0;
                int flag = 0;
                int inc = 1;
                DateTime date_watch;
                date_watch = date_start;
                while (date_watch <= date_finish)
                {
                    //date_watch = date_start.AddDays(frequency * inc);
                    string getQuery = "select * from main where resource=" + "'" + resource + "'" + " and city_from=" + "'" + city_from + "'" + " and city_to=" + "'" + city_to + "'" + " and (SELECT CAST(date_dep AS DATE))=" + "'" + date_watch.ToString("yyyy-MM-dd") + "'" + " and (SELECT CAST(date_collect AS DATE))=(SELECT CAST(date_dep AS DATE))" + ";";
                    MySqlConnection myConnection = new MySqlConnection(Connect);
                    MySqlCommand getQue = new MySqlCommand(getQuery, myConnection);
                    int hh = 0;
                    try
                    {

                        hh = (new DBManipulation()).getRecordNumber(Connect, "main", "where resource=" + "'" + resource + "'" + " and city_from=" + "'" + city_from + "'" + " and city_to=" + "'" + city_to + "'" + " and (SELECT CAST(date_dep AS DATE))=" + "'" + date_watch.ToString("yyyy-MM-dd") + "'" + " and (SELECT CAST(date_collect AS DATE))=(SELECT CAST(date_dep AS DATE))");

                        if (hh != 0)
                        {

                            myConnection.Open();
                            MySqlDataReader mreader = getQue.ExecuteReader();
                            while (mreader.Read())
                            {
                                price = mreader.GetInt32(2);

                                if (flag == 0)
                                {
                                    price_min = price;
                                    flag = 1;
                                }
                                else
                                {
                                    if (price < price_min) price_min = price;
                                }
                            }

                            mreader.Close();
                        }

                        myConnection.Close();


                    }
                    catch (Exception ee) { int er2 = 1; Console.WriteLine(ee.ToString()); }

                    //date_watch = date_start.AddDays(frequency * inc);
                    if (hh != 0)
                    {
                        gl_stat.price[inc] = price_min;
                        gl_stat.date[inc] = date_watch;

                    }
                    Console.WriteLine(gl_stat.price[inc]);
                    Console.WriteLine(gl_stat.date[inc]);
                    price_min = 0;
                    flag = 0;
                    date_watch = date_start.AddDays(frequency * inc);
                    inc++;

                }
                gl_stat.count = inc;
                return gl_stat;
            }
        }
        public void formStatistic1(String Connect, int id_user)
        {
            DBManipulation m1 = new DBManipulation();
            MySqlConnection metaConnection = new MySqlConnection(Connect);
            int id_stat = m1.getRecordNumber(Connect, "statistic", "") + 1;
            int count_to = 0;
            int count_from = 0;
            String axis_x = "";
            String axis_y = "";
            String[] route;
            int goodcount = 0;
            int[] id_req;
            int k = m1.getRecordNumber(Connect, "request_user", "where id_user=" + id_user);
            if (k == 0)
            {
                Console.WriteLine("User "+id_user+" has no requests.");
            }
            else
            {
                id_req = new int[k];
                String selrequs = "select id_request from request_user where id_user=" + id_user + ";";
                MySqlCommand selectreqforuser = new MySqlCommand(selrequs, metaConnection);
                MySqlDataReader mread;
                try
                {
                    int gh = 0;
                    metaConnection.Open();
                    mread = selectreqforuser.ExecuteReader();
                    while (mread.Read())
                    {
                        id_req[gh] = mread.GetInt32(0);
                        gh++;
                    }
                    metaConnection.Close();
                }
                catch (Exception ee) { Console.WriteLine("Failed:StatisticMethod   " + ee.ToString()); Console.ReadKey(); }
                //int reqcount = getRecordNumber(Connect, "request", "where id_request=(select id_request from request_user where id_user=" + id_user + ") and status=2 and statistic=\"type4\";");

                //Console.WriteLine("Strok: "+reqcount);
                String searchPred = "where (id_request=";
                for (int l = 0; l < id_req.Length; l++)
                {
                    searchPred += "" + id_req[l] + " or id_request=";
                }
                searchPred += "" + id_req[0];
                searchPred += ") and status=2 and statistic=\"1type\"";
                searchPred += ";";

                RequestPattern[] status2Request = m1.selectFromRequest(Connect, searchPred);

                Global1 goodGlob = new Global1();
                for (int i = 0; i < status2Request.Length; i++)
                {

                    while (count_from < status2Request[i].city_from.Length)
                    {

                        while (count_to < status2Request[i].city_to.Length)
                        {

                            if (status2Request[i].city_from.Substring(count_from, 3).Equals(status2Request[i].city_to.Substring(count_to, 3)) == false)
                                goodcount++;

                            count_to += 3;

                        }
                        count_to = 0;
                        count_from += 3;
                    }
                    count_to = 0;
                    count_from = 0;
                    route = new String[5];
                    int route_count = 0;
                    while (count_from < status2Request[i].city_from.Length)
                    {
                        while (count_to < status2Request[i].city_to.Length)
                        {
                            if (status2Request[i].city_from.Substring(count_from, 3).Equals(status2Request[i].city_to.Substring(count_to, 3)) == false)
                            {


                                route[route_count] = "" + status2Request[i].city_from.Substring(count_from, 3) + "" + status2Request[i].city_to.Substring(count_to, 3) + "";

                                goodGlob = this.getDataForStat1(status2Request[i].resource, status2Request[i].city_from.Substring(count_from, 3), status2Request[i].city_to.Substring(count_to, 3), status2Request[i].day_start, status2Request[i].day_finish, status2Request[i].frequency);
                                if (goodGlob == null)
                                {
                                    Console.WriteLine("Global1:null");
                                }
                                else
                                {
                                    for (int j = 1; j < goodGlob.date.Length; j++)
                                    {
                                        axis_x += "" + goodGlob.date[j].ToString("yyyy-MM-dd") + ";";
                                        axis_y += "" + goodGlob.price[j] + ";";
                                    }


                                    String ins_req_stat = "insert into request_statistic(id_request, id_statistic) values (" + status2Request[i].id_request + ", " + id_stat + ");";

                                    String ins_stat = "insert into statistic(id_statistic, route, axis_x, axis_y) values (" + id_stat + ", \"" + route[route_count] + "\", \"" + axis_x + "\", \"" + axis_y + "\");";

                                    MySqlCommand insert_r_s = new MySqlCommand(ins_req_stat, metaConnection);
                                    MySqlCommand insert_s = new MySqlCommand(ins_stat, metaConnection);
                                    try
                                    {
                                        metaConnection.Open();
                                        insert_s.ExecuteNonQuery();
                                        insert_r_s.ExecuteNonQuery();
                                        axis_x = "";
                                        axis_y = "";
                                        metaConnection.Close();
                                    }
                                    catch (Exception ee) { Console.WriteLine("Failed:StatisticMethod   " + ee.ToString()); Console.ReadKey(); }
                                    route_count++;
                                    id_stat++;
                                }
                            }
                            count_to += 3;
                        }
                        count_to = 0;
                        count_from += 3;

                    }
                }
            }

        }
        
    }
}
